$(function() {
    $("select").comboSelect();

    $("#select_repo").change(function() {
        var repo_name = $(this).children('option:selected').data('repo_name');
        var push_engineer_array =  repo_owner_list[repo_name]['repo_owner'];

        if (repo_owner_list[repo_name]['release_type'] != 0) {
            is_check_branch_name = false;
            $('#inputBranch').attr('placeholder', '分支名称，无验证，最多32个字符');
        } else {
            is_check_branch_name = true;
            $('#inputBranch').attr('placeholder', '分支名称，必须以_dev为结尾，最多32个字符');
        }

        $("#select_push_engineer").html('');
        $.each(push_engineer_array, function(key, value) {
            $("#select_push_engineer").append('<option value="'+value+'">'+value+'</option>');
        });
        $("#select_push_engineer").comboSelect();
    });

    $('#select_repo').change();

    $('#check_commit_id').click(function () {
        if ($(this).hasClass('disabled')) {
            return false;
        }
        if (!checkEmpty($("#inputBranch"))) {
            return false;
        }

        if (is_check_branch_name) {
            var branch_expr = /^\d{6}\_.*\_dev$/;
            if (!checkExpression(branch_expr, $('#inputBranch'))) {
                return false;
            }
        }

        if (!checkLength($('#inputBranch'), 32)) {
            return false;
        }
        if (!checkEmpty($("#inputCommitId"))) {
            return false;
        }

        $(this).addClass('disabled');

        var commit_id = $("#inputCommitId").val();
        var branch_name = $('#inputBranch').val();
        var repo_name = $('#select_repo').children('option:selected').data('repo_name');
        var url = "/gitool/check_commit_id_ajax";
        $.post(url, {commit_id: commit_id, branch_name: branch_name, repo_name: repo_name}, function(data) {
            if(data.status == 'success') {
                $("#inputCommitId").parent().removeClass('has-error has-feedback'); 
                $("#inputCommitId").parent().addClass('has-success has-feedback'); 
                $('#CommitIdStatusIcon').removeClass('glyphicon-remove');
                $('#CommitIdStatusIcon').addClass('glyphicon-ok');
                $('#CommitIdStatus').html('(success)');
                is_check_commit_id = true;
                $('#check_commit_id').removeClass('disabled');
            } else {
                $("#inputCommitId").parent().removeClass('has-success has-feedback'); 
                $("#inputCommitId").parent().addClass('has-error has-feedback'); 
                $('#CommitIdStatusIcon').removeClass('glyphicon-ok');
                $('#CommitIdStatusIcon').addClass('glyphicon-remove');
                $('#CommitIdStatus').html('(error)');
                is_check_commit_id = false;
                $('#check_commit_id').removeClass('disabled');
                return false;
            }

        }, 'json');
        return true;
        
    });

    $("#submit").click(function() {
        if ($(this).hasClass('disabled')) {
            return false;
        }
        $(this).addClass('disabled');

        if (!checkEmpty($("#inputAuthor"))) {
            return false;
        }

        if (!checkEmpty($("#inputBranch"))) {
            return false;
        }

        if (!checkLength($('#inputBranch'), 32)) {
            return false;
        }

        if (is_check_branch_name) {
            var branch_expr = /^\d{6}\_.*\_dev$/;
            if (!checkExpression(branch_expr, $('#inputBranch'))) {
                return false;
            }
        }

        if (!checkEmpty($("#inputCommitId"))) {
            return false;
        }

        if (!is_check_commit_id) {
            showError('请先检查commit id');
            return false;
        }

        if (!checkEmpty($("#inputCommitMsg"))) {
            return false;
        }

        if (!checkEmpty($("#inputReview"))) {
            return false;
        }

        var url_expr = /^((http|https):\/\/)?[\w-_.]+(\/[\w-_#]+)*\/?$/;
        if (!checkExpression(url_expr, $('#inputReview'))) {
            return false;
        }

        if ($('#inputJira').val() != '') {
            if (!checkExpression(url_expr, $('#inputJira'))) {
                return false;
            }
        }

        var url = "/gitool/new_task_ajax";
        $.post(url, $('#form').serialize(), function(data) {
            if(data.status == 'success') {
                location.href = "/gitool/task_detail?branch_id="+data.data['branch_id'];
                return false;
            } else {
                showError(data.message);
                return false;
            }

        }, 'json');
        return false;
    });

    $('.add_one').click(function() {
        var root = $(this).parent().parent().parent();
        var new_row = root.children().eq(0).clone();
        new_row.children().first().remove()
        new_row.children().last().remove()

        // 针对select 做的特殊处理
        var select = new_row.find('.combo-select').children().first();
        if (select) {
            new_row.find('.combo-select').remove();
            new_row.children().last().append(select);
        }

        new_row.children().first().addClass('col-lg-offset-2');
        new_row.find('input').val('');
        root.append(new_row);
        $("select").comboSelect();
        return false;
    });

    function checkEmpty (obj) {
        if (obj.val() == '') {
            var obj_id = obj.attr('id');
            var msg = $('label[for="'+obj_id+'"]').html() + '不能为空！';
            showError(msg);
            return false;
        }
        return true;
    }

    function checkLength (obj, len) {
        if (obj.val().length > len) {
            var obj_id = obj.attr('id');
            var msg = $('label[for="'+obj_id+'"]').html() + '不能超过' + len + '个字符';
            showError(msg);
            return false;
        }
        return true;
    }

    function checkExpression(expr, obj) {
        if (!expr.test(obj.val())) {
            var obj_id = obj.attr('id');
            var msg = $('label[for="'+obj_id+'"]').html() + '格式不正确！';
            showError(msg);
            return false;
        }
        return true;
    }

    function showError (msg) {
        $("#error_box").html(msg);
        $("#error_box").show('100', function() {
            $(this).delay(3000);
            $(this).hide('100', function() {
                $("#submit").removeClass('disabled');
            });
        });
    }

});